package c_utils2;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    static String driver;
    static String url;
    static String user;
    static String password;

    static {
        Properties pro = new Properties();
        try {
            pro.load(JDBCUtils.class.getResourceAsStream("c2.properties"));
            driver = pro.getProperty("driver");
            url = pro.getProperty("url") ;
            user = pro.getProperty("user");
            password = pro.getProperty("password");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection(){
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,password);
            return conn;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void close(Connection conn, Statement sta){
        if(sta != null){
            try {
                sta.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void close(Connection conn, Statement sta, ResultSet rs){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        close(conn, sta);
    }
}
